package evostar.controller;

import evostar.access.AccessToken;
import evostar.model.User;
import evostar.service.GoodsService;
import evostar.service.RedisService;
import evostar.VO.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(value = "/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ThymeleafViewResolver thymeleafViewResolver;
    @Autowired
    private RedisService redisService;

    @RequestMapping(value = "/list", produces = "text/html")
    @ResponseBody
    @AccessToken(needLogin = false, seconds = 30, maxCount = 5)
    public String list(User user, Model model, HttpServletRequest request, HttpServletResponse response) {

        String html = redisService.getValue(GoodsService.GoodList, String.class);
        if (!StringUtils.isEmpty(html)) {
            return html;
        }
        List<GoodsVO> goodsVOList = goodsService.listGoodsVO();
        model.addAttribute("goodsList", goodsVOList);
        model.addAttribute("user", user);

        WebContext context = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goods_list", context);
        if (!StringUtils.isEmpty(html)) {
            redisService.setValue(GoodsService.GoodList, html, 60);
        }
        return html;
    }


    @RequestMapping(value = "/detail/{goodsId}", produces = "text/html")
    @ResponseBody
    public String detail(User user, Model model, @PathVariable("goodsId") int goodId, HttpServletRequest request, HttpServletResponse response) {
        model.addAttribute("user", user);

        String html = redisService.getValue(GoodsService.GoodDetail + goodId, String.class);
        if (!StringUtils.isEmpty(html)) {
            return html;
        }
        GoodsVO goods = goodsService.getGoodsByGoodsId(goodId);
        model.addAttribute("goods", goods);

        int seckillStatus = 0; // 0未开始，1进行中，2已结束
        int remainSeconds = 0; //距离秒杀的秒数

        long startAt = goods.getStartTime().getTime();
        long endAt = goods.getEndTime().getTime();
        long now = System.currentTimeMillis();

        if (now < startAt) {
            //没开始,到计时中
            seckillStatus = 0;
            remainSeconds = (int) (startAt - now) / 1000;
        } else if (now > endAt) {
            seckillStatus = 2;
            remainSeconds = -1;
        } else {
            seckillStatus = 1;
            remainSeconds = 0;
        }
        model.addAttribute("seckillStatus", seckillStatus);
        model.addAttribute("remainSeconds", remainSeconds);

        WebContext context = new WebContext(request, response, request.getServletContext(), request.getLocale(), model.asMap());
        html = thymeleafViewResolver.getTemplateEngine().process("goods_detail", context);
        if (!StringUtils.isEmpty(html)) {
            redisService.setValue(GoodsService.GoodDetail + goodId, html, 60);
        }
        return html;
    }
}
